package edu.uoc.tdp.pac4.client.maintenance;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import edu.uoc.tdp.pac4.beans.Course;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.MantenimentInterface;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;


public class CourseEditWindow extends JFrame {
	//MantenimentInterface mantInterf=new MantenimentImpl();
	//private static final long serialVersionUID = 1L;
	
	
	private static final long serialVersionUID = 1L;
	
	private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
	private ClientConnectionHelper connectionHelper_;
	private MantenimentInterface maintenance;

	private Course course;
	
	/**
	 * 
	 */
	private JTextField tfId;
	private JTextField tfName;
	private JTextField tfReqAttend;
	//private JTextArea tABibl, tAPlaDoc;	
	private JTextField tfCredits;
	private JTextField tfPlaEst;

	private final ButtonGroup buttonGroup = new ButtonGroup();
	private ButtonGroup groupStatus = new ButtonGroup();

	private JTextArea taBibliografia;

	private JTextArea taEducationPlan;

	//Date avui = new Date(System.currentTimeMillis());
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		LanguageUtils.setDefaultLanguage("i18n/messages");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CourseEditWindow frame = new CourseEditWindow(new ClientConnectionHelper());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @param connectionHelper_ 
	 */
	
	public CourseEditWindow(ClientConnectionHelper connectionHelper_)
			throws HeadlessException {
		this(null, connectionHelper_);
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public CourseEditWindow(final Course course, ClientConnectionHelper connectionHelper_) {

		this.course = course;
		this.connectionHelper_ = connectionHelper_;
		this.maintenance=connectionHelper_.getMaintenanceManager();
		
		
		setTitle("eAssistencia - Course edit");
		getContentPane().setLayout(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 565);
		
		
		
		JPanel panelSup = new JPanel();
		panelSup.setBounds(0, 0, 442, 517);
		getContentPane().add(panelSup);
		GridBagLayout gbl_panelSup = new GridBagLayout();
		gbl_panelSup.columnWidths = new int[] {5, 90, 90, 90};
		gbl_panelSup.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, 0.0};
		gbl_panelSup.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0};
		panelSup.setLayout(gbl_panelSup);
		
		JLabel labelId = new JLabel(" Id");
		labelId.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelId = new GridBagConstraints();
		gbc_labelId.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelId.anchor = GridBagConstraints.WEST;
		gbc_labelId.insets = new Insets(0, 0, 5, 5);
		gbc_labelId.gridx = 1;
		gbc_labelId.gridy = 0;
		panelSup.add(labelId, gbc_labelId);
		
		tfId = new JTextField();
		GridBagConstraints gbc_tfId = new GridBagConstraints();
		gbc_tfId.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfId.insets = new Insets(0, 0, 5, 5);
		gbc_tfId.gridx = 2;
		gbc_tfId.gridy = 0;
		panelSup.add(tfId, gbc_tfId);
		tfId.setColumns(1);
		
		JLabel labelName = new JLabel(LanguageUtils.getMessage("client.maintenance.usermanag.labelName"));
		labelName.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelName = new GridBagConstraints();
		gbc_labelName.fill = GridBagConstraints.HORIZONTAL;
		gbc_labelName.anchor = GridBagConstraints.WEST;
		gbc_labelName.insets = new Insets(0, 0, 5, 5);
		gbc_labelName.gridx = 1;
		gbc_labelName.gridy = 1;
		panelSup.add(labelName, gbc_labelName);
		
		tfName = new JTextField();
		GridBagConstraints gbc_tfName = new GridBagConstraints();
		gbc_tfName.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfName.insets = new Insets(0, 0, 5, 5);
		gbc_tfName.gridx = 2;
		gbc_tfName.gridy = 1;
		panelSup.add(tfName, gbc_tfName);
		tfName.setColumns(10);
		
		
		JLabel labelAttend = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.labelAttend"));
		labelAttend.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelAttend = new GridBagConstraints();
		gbc_labelAttend.anchor = GridBagConstraints.WEST;
		gbc_labelAttend.insets = new Insets(0, 0, 5, 5);
		gbc_labelAttend.gridx = 1;
		gbc_labelAttend.gridy = 2;
		panelSup.add(labelAttend, gbc_labelAttend);
		
		tfReqAttend = new JTextField();
		GridBagConstraints gbc_tfReqAttend = new GridBagConstraints();
		gbc_tfReqAttend.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfReqAttend.insets = new Insets(0, 0, 5, 5);
		gbc_tfReqAttend.gridx = 2;
		gbc_tfReqAttend.gridy = 2;
		panelSup.add(tfReqAttend, gbc_tfReqAttend);
		tfReqAttend.setColumns(10);
		
		JLabel labelCred = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.labelCred"));
		labelCred.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelCred = new GridBagConstraints();
		gbc_labelCred.anchor = GridBagConstraints.WEST;
		gbc_labelCred.insets = new Insets(0, 0, 5, 5);
		gbc_labelCred.gridx = 1;
		gbc_labelCred.gridy = 3;
		panelSup.add(labelCred, gbc_labelCred);
		
		
		
		
		
		tfCredits = new JTextField();
		tfCredits.setColumns(10);
		GridBagConstraints gbc_tfCredits = new GridBagConstraints();
		gbc_tfCredits.insets = new Insets(0, 0, 5, 5);
		gbc_tfCredits.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfCredits.gridx = 2;
		gbc_tfCredits.gridy = 3;
		panelSup.add(tfCredits, gbc_tfCredits);
		
				
		
		JLabel labelDegreePlan = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.pladoc"));
		labelDegreePlan.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelDegreePlan = new GridBagConstraints();
		gbc_labelDegreePlan.anchor = GridBagConstraints.WEST;
		gbc_labelDegreePlan.insets = new Insets(0, 0, 5, 5);
		gbc_labelDegreePlan.gridx = 1;
		gbc_labelDegreePlan.gridy = 4;
		panelSup.add(labelDegreePlan, gbc_labelDegreePlan);
		
		
		tfPlaEst = new JTextField();
		tfPlaEst.setColumns(10);
		GridBagConstraints gbc_tfPlaEst = new GridBagConstraints();
		gbc_tfPlaEst.insets = new Insets(0, 0, 5, 5);
		gbc_tfPlaEst.fill = GridBagConstraints.HORIZONTAL;
		gbc_tfPlaEst.gridx = 2;
		gbc_tfPlaEst.gridy = 4;
		panelSup.add(tfPlaEst, gbc_tfPlaEst);
		
		JLabel labelEducationalPlan = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.labelPlaEst"));
		labelEducationalPlan.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_labelEducationalPlan = new GridBagConstraints();
		gbc_labelEducationalPlan.anchor = GridBagConstraints.WEST;
		gbc_labelEducationalPlan.insets = new Insets(0, 0, 5, 5);
		gbc_labelEducationalPlan.gridx = 1;
		gbc_labelEducationalPlan.gridy = 5;
		panelSup.add(labelEducationalPlan, gbc_labelEducationalPlan);
		
		taEducationPlan = new JTextArea();
		GridBagConstraints gbc_tAEducationalPlan = new GridBagConstraints();
		gbc_tAEducationalPlan.gridwidth = 3;
		gbc_tAEducationalPlan.insets = new Insets(0, 0, 5, 0);
		gbc_tAEducationalPlan.fill = GridBagConstraints.BOTH;
		gbc_tAEducationalPlan.gridx = 2;
		gbc_tAEducationalPlan.gridy = 5;
		panelSup.add(taEducationPlan, gbc_tAEducationalPlan);
		
		JLabel lblBibliography = new JLabel(LanguageUtils.getMessage("client.maintenance.coursemanag.labelBibl"));
		lblBibliography.setHorizontalAlignment(SwingConstants.LEFT);
		GridBagConstraints gbc_lblBibliography = new GridBagConstraints();
		gbc_lblBibliography.anchor = GridBagConstraints.WEST;
		gbc_lblBibliography.insets = new Insets(0, 0, 5, 5);
		gbc_lblBibliography.gridx = 1;
		gbc_lblBibliography.gridy = 6;
		panelSup.add(lblBibliography, gbc_lblBibliography);
		
		taBibliografia = new JTextArea();
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridwidth = 3;
		gbc_textArea.insets = new Insets(0, 0, 5, 0);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 2;
		gbc_textArea.gridy = 6;
		panelSup.add(taBibliografia, gbc_textArea);
		
		

		JRadioButton rdbtnActive = new JRadioButton("active");
		GridBagConstraints gbc_rdbtnActive = new GridBagConstraints();
		gbc_rdbtnActive.anchor = GridBagConstraints.WEST;
		gbc_rdbtnActive.insets = new Insets(0, 0, 0, 5);
		gbc_rdbtnActive.gridx = 1;
		gbc_rdbtnActive.gridy = 7;
		panelSup.add(rdbtnActive, gbc_rdbtnActive);
		
		JRadioButton rdbtnInactive = new JRadioButton("inactive");
		GridBagConstraints gbc_rdbtnInactive = new GridBagConstraints();
		gbc_rdbtnInactive.anchor = GridBagConstraints.WEST;
		gbc_rdbtnInactive.gridx = 2;
		gbc_rdbtnInactive.gridy = 7;
		panelSup.add(rdbtnInactive, gbc_rdbtnInactive);
		
		groupStatus.add(rdbtnActive);
		groupStatus.add(rdbtnInactive);
		
		
		
		
		JButton btnSave = new JButton(LanguageUtils.getMessage("client.maintenance.classmanag.btnSave"));
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					Course courseX= new Course();
					//usuari.setContrasenya(txtPassword.getText());
					courseX.setCourseName(tfName.getText());
					courseX.setPlaDocent(tfPlaEst.getText());
					courseX.setReqAttendance(Integer.valueOf(tfReqAttend.getText()));
					courseX.setCredits(Integer.valueOf(tfReqAttend.getText()));
					courseX.setPlaEstudis(taEducationPlan.getText());
					courseX.setBibliography(taBibliografia.getText());
					
					if(course == null){
						createCourse(courseX);
					} else {
						courseX.setCourseCode(course.getCourseCode());
						updateCourse(courseX);
					}
					dispose();
				} catch (IllegalArgumentException ex) {
					JOptionPane.showMessageDialog(null, ex.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				} catch (RemoteException e1) {
					//LanguageUtils.getMessage("client.management.button.group.saved")
					e1.printStackTrace();
				} 
				catch (ParseException e1) {
					JOptionPane.showMessageDialog(null, e1.getMessage(), null,
							JOptionPane.INFORMATION_MESSAGE);			
				}
				
			}
		});
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.BOTH;
		gbc_btnSave.insets = new Insets(0, 0, 0, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 8;
		panelSup.add(btnSave, gbc_btnSave);
		


		
		JButton btnExit = new JButton(LanguageUtils.getMessage("client.maintenance.usermanag.btnCancel"));
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnExit.setForeground(Color.RED);
		GridBagConstraints gbc_btnExit = new GridBagConstraints();
		gbc_btnExit.insets = new Insets(0, 0, 0, 5);
		gbc_btnExit.fill = GridBagConstraints.BOTH;
		gbc_btnExit.gridx = 2;
		gbc_btnExit.gridy = 8;
		panelSup.add(btnExit, gbc_btnExit);
		

		
		
		if(course!=null) {
			System.out.println(course);
			tfId.setText(""+course.getCourseCode());//getDataBaixa().toString()
			tfId.setEnabled(false);
			tfName.setText(course.getCourseName());
			tfPlaEst.setText(course.getPlaDocent());
			tfReqAttend.setText(""+course.getReqAttendance());
			tfCredits.setText(""+course.getCredits());
			taBibliografia.setText(course.getBibliography());
			taEducationPlan.setText(course.getPlaEstudis());
			
			if(course.isActive()){
				rdbtnActive.setSelected(true);				
			} else {
				rdbtnInactive.setSelected(true);		
			}

			
		}
		
	}		
		

	
	private Course createCourse(Course courseX) throws RemoteException, ParseException {
		Course course = maintenance.createCourse(courseX);
		return courseX;		
	}
	
	protected void updateCourse(Course courseX) throws RemoteException {
		maintenance.updateCourse(courseX);
	}

		
	
}


